package com.visikard.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;

import com.visikard.ui.R;
import com.visikard.ui.activity.RedemptionActivity;
import com.visikard.ui.activity.RedemptionActivity.RedeemptionActivityEnum;
import com.visikard.widget.NavigationStateRelativeLayout;

public class RedeemptionFragment extends BaseFragment implements ViewClickable{
	
	public static final String TAB_FRAGMENT_SLIDE_LAYOUT = "SlideOpenCameraFragment";
	public static final String TAB_FRAGMENT_SCAN_CODE = "ScanCodeFragment";
	public static final String TAB_FRAGMENT_INPUT_CODE = "InputCodeFragment";
	
	private LinearLayout parentOfOption;
	private NavigationStateRelativeLayout btnScanCode;
	private NavigationStateRelativeLayout btnInputCode;
	
	private FragmentManager fmManager;
	private FragmentTransaction fmTransaction;
	private Fragment saveFg;
	
	public static boolean isScanCode = false;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View parent = inflater.inflate(
				R.layout.redeemption_code_main_fragment, container, false);
		
		fmManager = getActivity().getSupportFragmentManager();
		
		parentOfOption = (LinearLayout) parent.findViewById(R.id.layout_top);

		btnScanCode = (NavigationStateRelativeLayout) parent
				.findViewById(R.id.vk_scan_barcode);
		btnInputCode = (NavigationStateRelativeLayout) parent
				.findViewById(R.id.vk_input_code);
		
		RedeemptionActivityEnum startupFragment = RedeemptionActivityEnum.SCANCODE;
		if(getArguments() != null && getArguments().getSerializable(RedemptionActivity.KEY_STARTUP_FRAGMENT) != null){
			startupFragment = (RedeemptionActivityEnum)getArguments().getSerializable(RedemptionActivity.KEY_STARTUP_FRAGMENT);			
		}
		switch(startupFragment){
		case SCANCODE:
			btnScanCode.setChecked(true);		
			putFragment(new SlideOpenCameraFragment(), TAB_FRAGMENT_SLIDE_LAYOUT);
			break;
		case TYPECODE:
			btnInputCode.setChecked(true);		
			putFragment(new InputCodeFragment(), TAB_FRAGMENT_INPUT_CODE);
			break;
		}
		
		return parent;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		if(saveFg instanceof ScanCodeFragment){
			putFragment(saveFg, TAB_FRAGMENT_SCAN_CODE);
		}else if(saveFg instanceof InputCodeFragment){
			putFragment(saveFg, TAB_FRAGMENT_INPUT_CODE);
		}
		if(isScanCode){
			isScanCode = false;
			removeFragment();
			putFragment(new SlideOpenCameraFragment(), TAB_FRAGMENT_SLIDE_LAYOUT);
		}
	}
	
	@Override
	public void onPause() {
		removeFragment();
		super.onPause();
	}
	
	/**
	 * put a fragment with UI
	 */
	public void putFragment(Fragment fm, String tag) {
		saveFg = fm;
		if(fmManager.findFragmentByTag(tag)== null){
			fmTransaction = fmManager.beginTransaction();
			fmTransaction.add(R.id.redeeption_code_content_cover, fm, tag);		
	//		fmTransaction.addToBackStack(null);
			fmTransaction.commit();
		}
	}
	
	public void removeFragment(){
		if(saveFg != null)
			fmManager.beginTransaction().remove(saveFg).commit();
	}
	
	@Override
	public void onViewClick(View v) {
		Fragment fm = fmManager.findFragmentById(R.id.redeeption_code_content_cover);
		switch (v.getId()) {
		case R.id.vk_scan_barcode:
			//hide keyboard here
			InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
					Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
			
			resetActivityOption(v.getId());
			if(!(fm instanceof ScanCodeFragment) && !(fm instanceof SlideOpenCameraFragment)){
				removeFragment();
				putFragment(new SlideOpenCameraFragment(), TAB_FRAGMENT_SLIDE_LAYOUT);
			}
			btnScanCode.setChecked(true);
			break;
		case R.id.vk_input_code:
			resetActivityOption(v.getId());
			if(!(fm instanceof InputCodeFragment)){
				removeFragment();
				putFragment(new InputCodeFragment(), TAB_FRAGMENT_INPUT_CODE);
			}
			btnInputCode.setChecked(true);
			break;
		default:
			break;
		}
	}
	
	public void resetActivityOption(int selectedId) {

		if (parentOfOption != null) {
			int count = parentOfOption.getChildCount();
			for (int i = 0; i < count; i++) {
				View v = parentOfOption.getChildAt(i);
				if (v instanceof NavigationStateRelativeLayout
						&& selectedId != v.getId()) {
					((NavigationStateRelativeLayout) v).setChecked(false);
				}
			}
		}
	}

}
